package cn.sptpc.servlet;

import cn.sptpc.service.UserService;
import cn.sptpc.service.impl.UserServiceI;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.commons.fileupload.FileUploadException;

@WebServlet("/UserServlet.do")
public class UserServlet extends HttpServlet {

    private UserService service = new UserServiceI();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/json;charset=gbk");
        String transaction = request.getParameter("transaction");
        if ("login".equals(transaction)){
            out.println(service.verifyUserLogin(request));
        }else if (transaction.equals("select")){
            if ("username".equals(request.getParameter("get"))){
                out.println(new Gson().toJson(request.getSession().getAttribute("user")));
            }else if("userInfo".equals(request.getParameter("get"))){
                out.println(service.getUserInfo(request));
            }
        }
        else if ("register".equals(transaction)){
            out.print(service.insertUser(request));
        }else if ("checkUser".equals(transaction)){
            out.print(service.checkUserId(request));
        }else if ("adel".equals(transaction)){
            out.print(service.deleteUser(request));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(request.getParameter("get").equals("img")){
            service.getUserFace(request,response);
        }
    }


}
